//
//  CZModalViewController.swift
//  GZWeibo007
//
//  Created by 陈宗毅 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
enum modalType: Int{

    case modalTypeSendStatu = 1
    case modalTypeCommentStatu = 2
    case modalTypeTransmitStatu = 3
    
}

class CZModalViewController: UIViewController {


    var status:CZStatus?
    
    var comtentType:modalType?
    
    init(comtent:modalType){
        comtentType = comtent
        super.init(nibName: nil, bundle: nil)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNav()
        
        prepareUI()

    }
  private func prepareUI(){
    
    self.view.addSubview(textView)
    
    //实现了文本到首行输入
    if self .respondsToSelector("setAutomaticallyAdjustsScrollViewInsets:"){
        self.automaticallyAdjustsScrollViewInsets = false
    }

    //布局
    //高300 屏幕宽度，x,y是0 
    textView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: view, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height),
        offset:CGPoint(x: 0, y: 64))

    }
    
    
  private func setupNav(){
//    navigationItem.titleView = custom
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle(rawValue: 0)!, target: self, action: "cancelClick")
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle(rawValue: 0)!, target: self, action: "sendClick")
    
    setupTitle()
    }
    
    
    func setupTitle(){
        
        var title = "发微博"
        if comtentType == modalType.modalTypeCommentStatu{
            title = "评论微博"
        }else if comtentType == modalType.modalTypeTransmitStatu
        {
            title = "转发微博"
        }

     if let name =    CZUserAccount.loadUserAccount()?.screen_name
     {

        let titleName = title + "\n" + name

        let attrString = NSMutableAttributedString(string: titleName)

        let label = UILabel()

        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)

        let nameRange = (titleName as NSString).rangeOfString(name)

        attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
        attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
        
        

        label.attributedText = attrString
        label.sizeToFit()
        
        navigationItem.titleView = label
     } else {

        navigationItem.title = title
        }
        
    }

    func cancelClick(){
        self.dismissViewControllerAnimated(true, completion: nil)
        self.textView .resignFirstResponder()
    }
    
     func sendClick(){
        let text = textView.text
        
        
        if text == nil || text == ""
        {
            SVProgressHUD.showInfoWithStatus("发送内容不能为空")
            return
        }
        
        //发布微博
        if comtentType == modalType.modalTypeSendStatu
        {
       
            CZNetworkTool.sharedInstance.sendStatusOnlyTitle(text) { (result, error) -> () in
                
                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("发送失败")
                }
                SVProgressHUD.showSuccessWithStatus("发送成功")
                print("result:\(result)")
            }
        }
        // 微博评论
        else if comtentType == modalType.modalTypeCommentStatu
        {
            //取得用户模型
            
            
            CZNetworkTool.sharedInstance.commentStatus(text, id: (status?.id)!, commentStatus: { (result, error) -> () in
                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("发送失败")
                }
                SVProgressHUD.showSuccessWithStatus("发送成功")
                print("result:\(result)")
            })
        }
        
        //转发微博
        else if comtentType == modalType.modalTypeTransmitStatu
        {
            
            
            CZNetworkTool.sharedInstance.relayStatus((status?.id)!, status: text, relaySuccessed: { (result, error) -> () in
                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("发送失败")
                }
                SVProgressHUD.showSuccessWithStatus("发送成功")
                print("result:\(result)")
            })
        }
        
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    

    lazy var textView:UITextView = {
        let textView = UITextView()
        textView.font = UIFont.systemFontOfSize(18)

        return textView
        }()
}
